package com.mz.ozy.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.mz.ozy.activity.me.MyOrderFragment;
import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.AssetsBean;
import com.mz.ozy.bean.MyOrderBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.AssetsModel;
import com.mz.ozy.mvp.view.MyOrderContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/12/10.
 */

public class MyOrderPresenter extends BasePresenterImpl<MyOrderContract.View> implements MyOrderContract.Presenter {

    private int mPage;
    private String mType = "0";

    @Override
    public void initData(Activity activity, int type, boolean b) {
        mPage = 1;
//        31我的订单（MyOrderServiceV1）
//        请求参数
//
//        参数名称	参数值	必须	说明
//        transCode		必须	MyOrderServiceV1
//        t		必须	token
//        pageNo			页码
//        type			类型:-1-我的订单 0-待付款 1-未发货 2-待收货 3-退货/售后

        switch (type) {
            case MyOrderFragment.TYPE_ALL:
                mType = "";
                break;
            case MyOrderFragment.TYPE_PAY:
                mType = "0";
                break;
            case MyOrderFragment.TYPE_SEND:
                mType = "1";
                break;
            case MyOrderFragment.TYPE_RECEIVE:
                mType = "2";
                break;
            case MyOrderFragment.TYPE_REFUND:
                mType = "8";
                break;
        }

        loadData(activity, b);
    }

    @Override
    public void loadMoreData(Activity activity) {
        ++mPage;
        loadData(activity, false);
    }

    @Override
    public void confirm(Activity activity, String tseq) {
        Map<String, String> maps = new HashMap<>();
        maps.put("tesq", tseq);
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10045, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<NetBean>(mView, false) {
                    @Override
                    public void onSuccesss(NetBean netBean) {

                        mView.onConfirmSuccess(netBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onConfirmError("");
                    }
                });
    }

    @Override
    public void cancelOrder(Activity activity, String tseq) {
        Map<String, String> maps = new HashMap<>();
        maps.put("tesq", tseq);
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10031, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<NetBean>(mView, false) {
                    @Override
                    public void onSuccesss(NetBean netBean) {
                        mView.onCancelOrderSuccess(netBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onCancelOrderError("");
                    }
                });
    }

    @Override
    public void delOrder(Activity activity, String tseq) {
//        Map<String, String> maps = new HashMap<>();
//        maps.put("transCode",Constants.TransCode.DelOrderV1);
//        maps.put("t", SharePreferencesData.getSharePreferencesData(SBMallApp.ctx, Constants.PREE.TOKEN_ID));
//        maps.put("tseq", tseq);
//        RetrofitUtils.getApiService(activity).post(maps)
//                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
//                .subscribe(new MyObserver<NetBean>(mView, false) {
//                    @Override
//                    public void onSuccesss(NetBean netBean) {
//
//                        mView.onDelOrderSuccess(netBean);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//                        mView.onDelOrderError("");
//                    }
//                });
    }

    private void loadData(Activity activity, boolean b) {
        Map<String, String> maps = new HashMap<>();
        maps.put("pageNo", String.valueOf(mPage));
        maps.put("pageSize", Constants.PageSize);
        if (!TextUtils.isEmpty(mType)) {
            maps.put("state", mType);
        }
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10020, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<MyOrderBean>(mView, b) {
                    @Override
                    public void onSuccesss(MyOrderBean myOrderBean) {

                        if (mPage == 1) {
                            mView.onInitSuccess(myOrderBean);
                            return;
                        }

                        if (myOrderBean.getPage() == null ||
                                myOrderBean.getPage().getPageItems() == null ||
                                myOrderBean.getPage().getPageItems().isEmpty()) {
                            mView.onLoadDataEnd();
                            return;
                        }

                        mView.onLoadMoreDataSuccess(myOrderBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (mPage == 1) {
                            mView.onInitError("");
                        } else {
                            mView.onLoadMoreDataError("");
                        }
                    }
                });
    }

    public void geAssetsInfo(Activity activity) {
        AssetsModel mModel = new AssetsModel();
        mModel.geAssetsInfo(activity)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<AssetsBean>(mView, false) {
                    @Override
                    public void onSuccesss(AssetsBean bean) {
                        mView.geAssetsInfoSuccess(bean);
                    }
                });
    }

    public void onPay(Context context, String id, String pwd, int i, int type) {
        String url = "";
        Map<String, String> maps = new HashMap<>();
        if (type == 1) {
            url = Constants.TransCode.TransCode10059;
            maps.put("type", i + "");
        } else {
            url = Constants.TransCode.TransCode10117;
            maps.put("payType", i + "");
        }

        maps.put("id", id);
        maps.put("payPwd", MD5Util.getMD5String(pwd));
        RetrofitUtils.getApiService(context).post(url, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView, true) {

                    @Override
                    public void onSuccesss(JSONObject orderBean) {
                        mView.onPaySuccess(orderBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onPayError("");
                    }
                });
    }
}
